package top.infopub.mgr.bpa.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.enums.bpa.BpaProcessDetailTypeEnum;
import top.infopub.enums.mkt.ProjectFileTypeEnum;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.bpa.dao.InnerAgrtDao;
import top.infopub.mgr.bpa.dao.SubpackSuppleDao;
import top.infopub.mgr.bpa.domain.BpaProjectDomain;
import top.infopub.mgr.bpa.domain.SubpackSuppleDomain;
import top.infopub.mgr.bpa.jsonVo.SubpackCotJsonVo;
import top.infopub.mgr.bpa.service.BpaProcessManageService;
import top.infopub.mgr.bpa.service.SubpackSuppleService;
import top.infopub.mgr.bpa.vo.SubpackSuppleVo;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.mkt.dao.ProjectAttachmentDao;
import top.infopub.model.bpa.BpaCompPlan;
import top.infopub.model.bpa.InnerListing;
import top.infopub.model.bpa.SubpackCotList;
import top.infopub.model.bpa.SubpackSupple;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.util.IdFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Service
public class SubpackSuppleServiceImpl extends GenericServiceImpl<SubpackSupple, String> implements SubpackSuppleService {

    @Autowired
    private SubpackSuppleDao subpackSuppleDao;

    @Autowired
    private InnerAgrtDao innerAgrtDao;

    @Autowired
    private ProjectAttachmentDao projectAttachmentDao;

    @Autowired
    private BpaProcessManageService bpaProcessManageService;


    @Override
    public GenericDao<SubpackSupple, String> getDao() {
        return subpackSuppleDao;
    }


    @Override
    public Page<SubpackSuppleDomain> selectByDomainWhere(Page<SubpackSuppleDomain> page,
                                                         SubpackSuppleVo subpackSuppleVo) throws ParseException {
        //处理时间范围
        if (!StringUtils.isEmpty(subpackSuppleVo.getDates()))
        {
            String[] splitTime = subpackSuppleVo.getDates().split("~");
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            subpackSuppleVo.setStartDate(format.parse(splitTime[0]));
            subpackSuppleVo.setEndDate(format.parse(splitTime[1]));
        }
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<SubpackSuppleDomain>)subpackSuppleDao.selectByDomainWhere(subpackSuppleVo);
    }

    @Override
    public RspVo selectProject() {
        List<BpaProjectDomain> bpaProjectDomains  = subpackSuppleDao.selectProject();
        return RspVo.getSuccessResponse(bpaProjectDomains);
    }

    @Override
    public RspVo queryPackDetail(String id) {
        SubpackCotJsonVo jsonVo = new SubpackCotJsonVo();
        //主表数据
        SubpackSupple subpackSupple = subpackSuppleDao.selectByPrimaryKey(id);
        jsonVo.setSubpackSupple(subpackSupple);
        //子项信息---
        List<InnerListing> innerListings = innerAgrtDao.selectListing(id,"4");
        jsonVo.setInnerListings(innerListings);

        //综合计划
        List<BpaCompPlan> compPlan = innerAgrtDao.selectPlan(id,"4");
        jsonVo.setCompPlan(compPlan);
        List<ProjectAttachment> attachmentList = projectAttachmentDao.queryByWhere(subpackSupple.getProCode(), ProjectFileTypeEnum.FILE_TYPE91.getCode(), null);
        jsonVo.setFileAtt(fileAttaListToArray(attachmentList));
        return RspVo.getSuccessResponse(jsonVo);
    }

    @Override
    @Transactional
    public RspVo saveAll(String bodyStr, Integer submitType, AuthorizationVO userDtl, SubpackSupple subpackSupple, List<InnerListing> innerListings, List<BpaCompPlan> compPlan, List<ProjectAttachment> attachmentList) {
        final String type = "4"; //分包补充协议
        if (StringUtils.isNotBlank(subpackSupple.getId())){//编辑
            subpackSupple.setUptBy(userDtl.getId());
            subpackSupple.setUptTime(new Date());
            subpackSuppleDao.updateByPrimaryKey(subpackSupple);
        }else{//新增
            subpackSupple.setId(IdFactory.getUUIDUpper());
            subpackSupple.setAddBy(userDtl.getId());
            subpackSupple.setAddTime(new Date());
            subpackSuppleDao.insertById(subpackSupple);
        }
            innerAgrtDao.deleteListing(subpackSupple.getId(),type);
            int seq = 0;
            for (InnerListing innerListing : innerListings) {
                innerListing.setInnerId(subpackSupple.getId());
                innerListing.setType(type);
                innerListing.setSeq(seq++);
            }
            innerAgrtDao.insertBatchListing(innerListings);
        //处理综合计划
        innerAgrtDao.deletePlan(subpackSupple.getId(),type);
        for (BpaCompPlan bpaCompPlan : compPlan) {
            bpaCompPlan.setContractId(subpackSupple.getId());
            bpaCompPlan.setType(type);
        }
        innerAgrtDao.insertBatchPlan(compPlan);

        //处理附件
        if (attachmentList!=null && attachmentList.size()>0){
            //批量插入附件信息
            projectAttachmentDao.deleteByWhere(subpackSupple.getProCode(), ProjectFileTypeEnum.FILE_TYPE87.getCode(), null);
            for (ProjectAttachment attachment : attachmentList) {
                attachment.setProCode(subpackSupple.getProCode());
            }
            projectAttachmentDao.insertBatch(attachmentList);
        }
        if (submitType==2){
            bpaProcessManageService.sendProcess(BpaProcessDetailTypeEnum.PRO_NODE_TYPE10, bodyStr, subpackSupple.getId(), subpackSupple.getProCode(), userDtl);
        }
        return RspVo.getSuccessResponse();
    }

    @Override
    public SubpackSupple selectBySguid(String id) {
        return subpackSuppleDao.selectByPrimaryKey(id);
    }

    public String[] fileAttaListToArray(List<ProjectAttachment> list) {
        if (list == null || list.size() <= 0) return null;
        String[] attaArr = new String[list.size()];
        int index = 0;
        for (ProjectAttachment pa : list) {
            StringBuffer tmp = new StringBuffer();
            tmp.append(pa.getSubTypes());
            tmp.append("|");
            tmp.append(pa.getFileName());
            tmp.append(pa.getFileSuffix());
            tmp.append("|");
            tmp.append(IdFactory.getUUIDSerialNumber());
            tmp.append("|");
            tmp.append(pa.getFilePath());
            attaArr[index] = tmp.toString();
            index++;
        }
        return attaArr;
    }
}
